package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper  setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐及其菜品关系
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //增加setmeal数据
        setmealMapper.insert(setmeal);
        //主键返回
        Long setmealId=setmeal.getId();
        //获取菜品与套餐数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //为其中数据添加套餐id
        for (SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }
        //将套餐与菜品关系存储
        setmealDishMapper.insertMealWithDish(setmealDishes);
    }

    /**
     * 分页条件查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageSelect(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageSelect(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        List<Long>statusList= setmealMapper.selectByIds(ids);
        log.info("状态:{}",statusList);
        if (statusList!=null && statusList.size()>0){
            for (Long status :statusList){
                if (status== 0){
                    setmealMapper.deleteByIds(ids);
                }else {
                    throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                }
            }
        }
    }

    /**
     * 修改套餐-数据回显
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectSetmealBId(Long id) {
        Setmeal setmeal=setmealMapper.selectSetmealById(id);
        List<SetmealDish> dishList=setmealDishMapper.selectDishBySetmealId(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(dishList);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //修改关联的setmeal_dish表(先删除原来的数据,再插入)
        //获取setmealid和setmealdish数据
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //删除旧数据
        setmealDishMapper.deleteBySetmealId(setmealId);
       //将sermealId存入SetmealDish集合
        for (SetmealDish setmealDish :setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }
        //新增setmeal_dish数据
        setmealDishMapper.insertMealWithDish(setmealDishes);


    }

    /**
     * 更新套餐状态
     * @param status
     * @param id
     */
    @Override
    public void statusManage(Integer status, Long id) {
        //启售套餐时,如果里面的菜品停售,则不许启售
        if (status == StatusConstant.ENABLE){
            //根据setmeal的id,查询dish
            List<Dish> dishes=dishMapper.selectDishBySetmealId(id);
            if (dishes!=null && dishes.size()>0){
                for (Dish dish :dishes){
                    if (dish.getStatus()==StatusConstant.DISABLE){
                        throw  new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        //如果执行到这里,则正常修改
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    /**
     * 根据categoryid查询套餐数据
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list=setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据setmealid查询菜品信息
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getDishBySetmealId(Long setmealId) {
        return setmealMapper.getDishBySetmealId(setmealId);
    }
}
